package arithmetic; import java.util.ArrayList; import java.util.List; public class MyComposite { static int level=-1; static int count = 1; static int selected = 3; public static void main(String[] args){ char buf[]={'a','b','c','d'}; List container = new ArrayList(); composite(container,buf,0,buf.length); } public static void composite(List container,char buf[],int start,int end){ if(container.size()== selected){ for(int i=0;i<container.size();i ){ System.out.print(container.get(i)); } System.out.println(); if(level>=selected-1){ level =selected-1; } System.out.println(count); count ; }else{ if(start>=end){ level--; }else{ for(int i=start;i<end;i ){ if(level == -1){ level = 0; } for(int m=0;m<selected-(level);m ){ if(container.size()>=selected-m){ container.remove(selected-1-m); } } level ; container.add(buf[i]); composite(container,buf,i 1,end); if(i 1>=end){ level--; } } } } } }
评论